package com.sky.service.impl;
import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;


    @Override
    public Integer add(ShoppingCartDTO shoppingCartDTO) {
        Integer tag = 0;
        // 1. 查询一下历史购物车,查询之前，需要把用户ID，放到shoppingCartDTO里面(需要自己在DTO增加userId)
        shoppingCartDTO.setUserId(BaseContext.getCurrentId());
        // 2. 如果基于当前用户Id\当前商品Id\当前口味 没有数据，插入
       List<ShoppingCart> shoppingCartList =  shoppingCartMapper.selectByList(shoppingCartDTO);
        if (shoppingCartList == null || shoppingCartList.size()==0 ){
            ShoppingCart shoppingCart = new ShoppingCart();
            // 先设置能设置字段
            shoppingCart.setUserId(shoppingCartDTO.getUserId());
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 再根据当前是否是菜品还是套餐，去查询对应的表，设置冗余字段
            if (shoppingCartDTO.getDishId() != null) {
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                // 根据菜品Id ,去查询菜品信息
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());  // 菜品或套餐名称
                shoppingCart.setAmount(dish.getPrice()); // 单价*数量 回头处理
                shoppingCart.setImage(dish.getImage()); // 图片
            }else{
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                // 根据套餐Id ,去查询套餐的信息
                Setmeal setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());  // 菜品或套餐名称
                shoppingCart.setAmount(setmeal.getPrice()); // 单价
                shoppingCart.setImage(setmeal.getImage()); // 图片
            }
            // 添加数据库
            log.info("[添加购物车之前] shoppingCart:{}",shoppingCart);
            tag = shoppingCartMapper.insert(shoppingCart);
        }else{
            // 有数据，更新份数
            ShoppingCart shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
           tag =  shoppingCartMapper.update(shoppingCart);
        }
        return tag;
    }

    @Override
    public List<ShoppingCart> findAll() {
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectByUserId(currentId);
    }

    @Override
    public Integer clean() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.deleteByUserId(userId);
    }
}
